package com.qf.commerce.controller;

import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.UserDto;
import com.qf.commerce.common.dto.UserSelectDto;
import com.qf.commerce.common.vo.UserVo;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 分页展示所有用户
     *
     * @param page page
     * @param size size
     * @return com.qf.commerce.resp.ResponseResult<com.github.pagehelper.PageInfo < com.qf.commerce.common.vo.UserVo>>
     * @author 358
     * @since JDK 1.8
     */
    @GetMapping("/list")
    public ResponseResult<PageInfo<UserVo>> all(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "6") Integer size) {
        return ResponseResult.success(userService.selectAll(page, size));
    }

    /**
     * 查看详细信息
     *
     * @param userId userId
     * @return com.qf.commerce.resp.ResponseResult<com.qf.commerce.common.vo.UserVo>
     * @author 358
     * @since JDK 1.8
     */
    @GetMapping("/selectById")
    public ResponseResult<UserVo> selectById(@RequestParam Integer userId) {
        try {
            return ResponseResult.success(userService.selectById(userId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 新建用户
     *
     * @param userDto userDto
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PostMapping("/creat")
    public ResponseResult<Integer> creatUser(@RequestBody UserDto userDto) {
        try {
            return ResponseResult.success(userService.insert(userDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 更新用户信息
     *
     * @param userDto userDto
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PutMapping("/update")
    public ResponseResult<Integer> update(@RequestBody UserDto userDto) {
        try {
            return ResponseResult.success(userService.update(userDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 更新用户状态
     *
     * @param userId userId
     * @param status status
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PutMapping("/status")
    public ResponseResult<Integer> changeStatus(@RequestParam("userId") Integer userId, @RequestParam("status") Integer status) {
        try {
            return ResponseResult.success(userService.updateStatus(userId, status));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 模糊查询用户
     *
     * @param dto dto
     * @return com.qf.commerce.resp.ResponseResult<com.github.pagehelper.PageInfo < com.qf.commerce.common.vo.UserVo>>
     * @author 358
     * @since JDK 1.8
     */
    @GetMapping("/select")
    public ResponseResult<PageInfo<UserVo>> select(@RequestBody UserSelectDto dto) {
        try {
            return ResponseResult.success(userService.select(dto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

}
